package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
         memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        //获取最近一年的月份
        List<String> months = DateUtils.getThisYearMonths();
        //创建存储每月会员人数的集合
        List<Long> memberCount = new ArrayList<>();
        //获取每个月对应的会员总数
        for (String month : months) {
            month += "-01";
            String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            long count = memberDao.getMemberCountByMonth(thisMonthLastDay);
            memberCount.add(count);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("months" , months);
        resultMap.put("memberCount" , memberCount);
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> getMemberReportBySex() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        long menNumber = memberDao.getMemberReportByMen();
        map1.put("name", "男");
        map1.put("value", menNumber);
        long womenNumber = memberDao.getMemberReportByWoMen();
        map2.put("name", "女");
        map2.put("value", womenNumber);
        mapList.add(map1);
        mapList.add(map2);
        return mapList;
    }

    @Override
    public List<Map<String, Object>> getMemberReportByAge() {
        //获取0-18、18-30、30-45、45以上的日期
        //当前时间
        String nowDate = DateUtils.parseDate2String(new Date());
        //0-18
        String eighteen = DateUtils.getAgeBeforeYear(18);
        long num1 = memberDao.getMemberReportBetweenAge(nowDate, eighteen);
        Map<String, Object> map1= new HashMap<>();
        map1.put("name", "0-18岁");
        map1.put("value", num1);
        //18-30
        String thirty = DateUtils.getAgeBeforeYear(30);
        long num2 = memberDao.getMemberReportBetweenAge(eighteen, thirty);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "18-30岁");
        map2.put("value", num2);
        //30-45、
        String fortyfive = DateUtils.getAgeBeforeYear(45);
        long num3 = memberDao.getMemberReportBetweenAge(thirty, fortyfive);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", "30-45岁");
        map3.put("value", num3);
        //45以上
        long num4 = memberDao.getMemberReportAfterAge(fortyfive);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("name", "45以上");
        map4.put("value", num4);

        List<Map<String, Object>> mapList = new ArrayList<>();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);


        return mapList;
    }

    @Override
    public Map<String, Object> getMemberReportByDate(Date start, Date end) {
        String yearStart = DateUtils.parseDate2String(start, "yyyy");
        String yearEnd = DateUtils.parseDate2String(end, "yyyy");
        String monthStart=DateUtils.parseDate2String(start, "MM");
        String monthEnd=DateUtils.parseDate2String(end, "MM");
        List<String> thisMonthDays=new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        Map<String,Object> resultMap = new HashMap<>();
        if (yearStart.equals(yearEnd)&&monthStart.equals(monthEnd)){

            thisMonthDays = DateUtils.getThisMonthDays(start);
            resultMap.put("months" , thisMonthDays);
            for (String thisMonthDay : thisMonthDays) {
                long count= memberDao.getMemberReportByDate(thisMonthDay);
                memberCount.add(count);
            }
            resultMap.put("memberCount" , memberCount);
            return resultMap;
        }else if (yearStart.equals(yearEnd)&&!(monthStart.equals(monthEnd))){
            int a = Integer.parseInt(monthStart);
            int b = Integer.parseInt(monthEnd);
            for (int i = a; i <= b; i++) {
                thisMonthDays.add(yearEnd+i);
                String thisMonthFirst=null;
                if (i<0){
                    thisMonthFirst=yearStart+"-0"+i+"-01";
                }else {
                    thisMonthFirst=yearStart+"-"+i+"-01";
                }
                Date thisMonthLastDayDate = DateUtils.getThisMonthLastDay(thisMonthFirst);
                String thisMonthLastDay = DateUtils.parseDate2String(thisMonthLastDayDate);
                long count= memberDao.getMemberLimitByDate(thisMonthFirst,thisMonthLastDay);
                memberCount.add(count);
            }
            resultMap.put("months" , thisMonthDays);
            resultMap.put("memberCount" , memberCount);
            return resultMap;
        }else if (!(yearStart.equals(yearEnd))){
            int a = Integer.parseInt(yearStart);
            int b = Integer.parseInt(yearEnd);
            for (int i = a; i <= b; i++) {
                thisMonthDays.add(String.valueOf(i));
                String thisYearFirstDay=i+"-01-01";
                String thisYearLastDay=i+"-12-31";
                long count= memberDao.getMemberLimitByDate(thisYearFirstDay,thisYearLastDay);
                memberCount.add(count);
            }
            resultMap.put("months" , thisMonthDays);
            resultMap.put("memberCount" , memberCount);
            return resultMap;
        }

        return resultMap;
    }
}
